package DatabaseManaging;

	import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

	 
	public class DatabaseManager
	{
	    public static void main(String[] args) throws SQLException 
	    {
	        System.out.println("Starting..");
	    	DatabaseManager db = new DatabaseManager();
	       // Connection conn = db.connect("jdbc:mysql://127.0.0.1:3306/ea","root","");
	    	Properties prop=new Properties();
	    	try {
				prop.load(DatabaseManager.class.getClassLoader().getResourceAsStream("DBproperties"));
				Connection conn=db.connect(prop.getProperty("jdbc.url"), prop.getProperty("jdbc.username"), prop.getProperty("db_password"));
				String query="UPDATE akn_product_dim SET product_abbr1='NOT PARSED' WHERE product_dim_id =1";
				Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
				stmt.executeUpdate(query);	
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       // Connection conn = db.connect("jdbc:mysql://127.0.0.1:13306/ea","analytics","Password!");
	       // System.out.println("Connected.Uploading Files to the database..");
	        // db.importData(conn,"VolumeFilter","akn_traffic_fact");
	      // db.importDataFile(conn,new File(args[0]),"akn_feature_fact");
//	       db.importData(conn,"NoResultQueryFilter","akn_NoQueryResult_fact");
//	       db.importData(conn,"SessionFilter","akn_session_fact");
//	       db.importDataAnual(conn,"WordFilter","akn_word_fact");
//	       for(int i=13;i<21;i++){ 
//		       db.importData(conn,Integer.toString(i), "VolumeFilter","akn_traffic_fact");
//		       db.importData(conn,Integer.toString(i), "QueryFilter","akn_query_fact");
//		       db.importData(conn,Integer.toString(i), "NoResultQueryFilter","akn_NoQueryResult_fact");
//		       db.importData(conn,Integer.toString(i), "SessionFilter","akn_session_fact");
//		       db.importData(conn,Integer.toString(i), "WordFilter","akn_word_fact");
//	       }

	       System.out.println("Finished!");
	    }
	 
 
	    public Connection connect(String db_connect_str, 
	  String db_userid, String db_password)
	    {
	        Connection conn;
	        try 
	        {
	            Class.forName("com.mysql.jdbc.Driver").newInstance();
	 
	            conn = DriverManager.getConnection(db_connect_str, db_userid, db_password);
	        
	        }
	        catch(Exception e)
	        {
	            e.printStackTrace();
	            conn = null;
	        }
	 
	        return conn;    
	    }
	    
	    
	    public void importData(Connection conn,String filename,String filter,String tablename)
	    {
	        Statement stmt;
	        String query;
	 
	        try
	        {
	            stmt = conn.createStatement(
	    ResultSet.TYPE_SCROLL_SENSITIVE,
	    ResultSet.CONCUR_UPDATABLE);
	            query = "LOAD DATA LOCAL INFILE '/home/leo/GlobantAutodesk/Filters/2012-10-"+filename+"/"+filter+"/part-r-00000' " +
	            		"INTO TABLE "+tablename+";";
	            stmt.executeUpdate(query);
	            System.out.println(filename+"-"+filter+". Success");
	        }
	        catch(Exception e)
	        {
	            e.printStackTrace();
	            stmt = null;
	        }
	    }
	    public void importDataFile(Connection conn,String file,String tablename)
	    {
	        Statement stmt;
	        String query;
	 
	        try
	        {
	            stmt = conn.createStatement(
	    ResultSet.TYPE_SCROLL_SENSITIVE,
	    ResultSet.CONCUR_UPDATABLE);
	            query = "LOAD DATA LOCAL INFILE '"+file+"' " +
	            		"INTO TABLE "+tablename+";";
	            System.out.println(query);
	            stmt.executeUpdate(query);
	        }
	        catch(Exception e)
	        {
	            e.printStackTrace();
	            stmt = null;
	        }
	    }
	    
	    public void executeQuery(String inputquery)
	    {
	        Statement stmt;
	        String query;
	        try
	        {
		        Connection conn = this.connect("jdbc:mysql://localhost:3306/ea","root","");
	            stmt = conn.createStatement(
	    ResultSet.TYPE_SCROLL_SENSITIVE,
	    ResultSet.CONCUR_UPDATABLE);
	   
	            stmt.executeUpdate(inputquery);
	            System.out.println("Success");
	        }
	        catch(Exception e)
	        {
	            e.printStackTrace();
	            stmt = null;
	        }
	    }
	    
	    
	    
	}


